<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书id" prop="bookId">
        <el-input
          v-model="queryParams.bookId"
          placeholder="请输入图书id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借书管理员id" prop="borrowAdminid">
        <el-input
          v-model="queryParams.borrowAdminid"
          placeholder="请输入借书管理员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="借书时间" prop="borrowTime">
        <el-date-picker clearable
          v-model="queryParams.borrowTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择借书时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="还书管理员id" prop="returnAdminid">
        <el-input
          v-model="queryParams.returnAdminid"
          placeholder="请输入还书管理员id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="还书时间" prop="returnTime">
        <el-date-picker clearable
          v-model="queryParams.returnTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择还书时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否还书" prop="returnIs">
        <el-select v-model="queryParams.returnIs" placeholder="请选择是否还书" clearable>
          <el-option
            v-for="dict in dict.type.library_return"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上链状态" prop="borrowTobc">
        <el-select v-model="queryParams.borrowTobc" placeholder="请选择上链状态" clearable>
          <el-option
            v-for="dict in dict.type.library_tobc"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="备用字段1" prop="tag1">-->
<!--        <el-input-->
<!--          v-model="queryParams.tag1"-->
<!--          placeholder="请输入备用字段1"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用字段2" prop="tag2">-->
<!--        <el-input-->
<!--          v-model="queryParams.tag2"-->
<!--          placeholder="请输入备用字段2"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['librarysystem:libraryborrow:add']"
        >借书</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['librarysystem:libraryborrow:return']"
        >还书</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-upload"
          size="mini"
          :disabled="multiple"
          @click="handleUp"
          v-hasPermi="['librarysystem:libraryborrow:block']"
        >上链</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['librarysystem:libraryborrow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="libraryborrowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="借书信息id" align="center" prop="borrowId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="图书id" align="center" prop="bookId" />
      <el-table-column label="借书管理员id" align="center" prop="borrowAdminid" />
      <el-table-column label="借书时间" align="center" prop="borrowTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.borrowTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="还书管理员id" align="center" prop="returnAdminid" />
      <el-table-column label="还书时间" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否还书" align="center" prop="returnIs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.library_return" :value="scope.row.returnIs"/>
        </template>
      </el-table-column>
      <el-table-column label="上链状态" align="center" prop="borrowTobc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.library_tobc" :value="scope.row.borrowTobc"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="备用字段1" align="center" prop="tag1" />-->
<!--      <el-table-column label="备用字段2" align="center" prop="tag2" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['librarysystem:libraryborrow:edit']"
          >通过借书请求</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleUpdate2(scope.row)"
            v-hasPermi="['librarysystem:libraryborrow:edit']"
          >通过还书请求</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改借书信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户id" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入用户id" />-->
<!--        </el-form-item>-->
        <el-form-item label="图书id" prop="bookId">
          <el-input v-model="form.bookId" placeholder="请输入图书id" />
        </el-form-item>
<!--        <el-form-item label="借书管理员id" prop="borrowAdminid">-->
<!--          <el-input v-model="form.borrowAdminid" placeholder="请输入借书管理员id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="借书时间" prop="borrowTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.borrowTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择借书时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="还书管理员id" prop="returnAdminid">-->
<!--          <el-input v-model="form.returnAdminid" placeholder="请输入还书管理员id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="还书时间" prop="returnTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.returnTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择还书时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否还书" prop="returnIs">-->
<!--          <el-select v-model="form.returnIs" placeholder="请选择是否还书">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.library_return"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="上链状态" prop="borrowTobc">-->
<!--          <el-select v-model="form.borrowTobc" placeholder="请选择上链状态">-->
<!--            <el-option-->
<!--              v-for="dict in dict.type.library_tobc"-->
<!--              :key="dict.value"-->
<!--              :label="dict.label"-->
<!--              :value="dict.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用字段1" prop="tag1">-->
<!--          <el-input v-model="form.tag1" placeholder="请输入备用字段1" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备用字段2" prop="tag2">-->
<!--          <el-input v-model="form.tag2" placeholder="请输入备用字段2" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLibraryborrow, getLibraryborrow, upLibraryborrow, addLibraryborrow,
  updateLibraryReturn, updateLibraryborrow, updateLibraryborrow2, addLibrarycredit
} from "@/api/librarysystem/libraryborrow";

export default {
  name: "Libraryborrow",
  dicts: ['library_tobc', 'library_return'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      bookId: null,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 借书信息管理表格数据
      libraryborrowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        bookId: null,
        borrowAdminid: null,
        borrowTime: null,
        returnAdminid: null,
        returnTime: null,
        returnIs: null,
        borrowTobc: null,
        tag1: null,
        tag2: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        bookId: [
          { required: true, message: "图书id不能为空", trigger: "blur" }
        ],
        borrowAdminid: [
          { required: true, message: "借书管理员id不能为空", trigger: "blur" }
        ],
        borrowTime: [
          { required: true, message: "借书时间不能为空", trigger: "blur" }
        ],
        returnIs: [
          { required: true, message: "是否还书不能为空", trigger: "change" }
        ],
        borrowTobc: [
          { required: true, message: "上链状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询借书信息管理列表 */
    getList() {
      this.loading = true;
      listLibraryborrow(this.queryParams).then(response => {
        this.libraryborrowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        borrowId: null,
        userId: null,
        bookId: null,
        borrowAdminid: null,
        borrowTime: null,
        returnAdminid: null,
        returnTime: null,
        returnIs: null,
        borrowTobc: null,
        tag1: null,
        tag2: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.borrowId)
      this.bookId = selection.map(item => item.bookId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加借书信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate() {
      this.reset();
      const borrowId = this.ids;
      const bookId = this.bookId;
      const _this=this;
      getLibraryborrow(borrowId).then(response => {
        _this.form = response.data;
      });
      this.$modal.confirm('是否确认将图书编号为"' + bookId + '"的图书归还？').then(function() {
        return updateLibraryReturn(_this.form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("归还成功，等待管理员审核");
      }).catch(() => {});
    },
    handleUpdate1(row) {
      const userId = row.userId;
      const bookId = row.bookId;
      const _this=this;
      getLibraryborrow(row.borrowId).then(response => {
        _this.form = response.data;
      });
      this.$modal.confirm('是否确认通过用户编号为"' + userId + '"，图书编号为"' + bookId + '"的借书请求？').then(function() {
        return updateLibraryborrow(_this.form);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("通过成功");
      }).catch(() => {});
    },
    handleUpdate2(row) {
      const userId = row.userId;
      const bookId = row.bookId;
      const _this=this;
      getLibraryborrow(row.borrowId).then(response => {
        _this.form = response.data;
      });
      this.$modal.confirm('是否确认通过用户编号为"' + userId + '"，图书编号为"' + bookId + '"的还书请求？').then(function() {
        return updateLibraryborrow2(_this.form);
      }).then(() => {
        addLibrarycredit(row.borrowId);
        this.getList();
        this.$modal.msgSuccess("通过成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.borrowId != null) {
            updateLibraryborrow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLibraryborrow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 上链按钮操作 */
    handleUp(row) {
      const borrowIds = row.borrowId || this.ids;
      this.$modal.confirm('是否确认上链借书信息管理编号为"' + borrowIds + '"的数据项？').then(function() {
        return upLibraryborrow(borrowIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("上链成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('librarysystem/libraryborrow/export', {
        ...this.queryParams
      }, `libraryborrow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
